/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.dao.util.MySqlDataAccessHelper;
import model.pojo.Catalogue;

/**
 *
 * @author TiOc
 */
public class CatalogueDAO 
{
    public static ArrayList<Catalogue> getCatalogue()
    {
        ArrayList<Catalogue> ds = new ArrayList<Catalogue>();
        String sql = "SELECT * FROM Catalogue As cl WHERE cl.isPublish = '1' ORDER BY cl.idBrandName";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql); 
        try
        {
            while(rs.next())
            {
                Catalogue cl = new Catalogue();
                cl.setIdCatalogue(rs.getInt("idCatalogue"));
                cl.setNameCatalogue(rs.getString("nameCatalogue"));
                cl.setLink(rs.getString("link"));
                cl.setIsPublish(rs.getInt("isPublish"));
                ds.add(cl);
            }
        }
        catch(SQLException ex)
        {
            System.out.println(ex.getMessage());
        }
        helper.close();
        
        return ds;
    }
    
    public static int laySoLuongSanPham(int idCatalogue)
    {
        String sql = String.format("SELECT COUNT(*) As SoLuong FROM Product WHERE idCatalogue = %d", idCatalogue);
        int soLuong = -1;
        try
        {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while(rs.next())
            {
                soLuong = rs.getInt("SoLuong");
            }
            helper.close();
        }
        catch(SQLException ex)
        {
            System.out.print(ex.getMessage());
        }
        return soLuong;
    }
    
    //Tinh so luong trang san pham theo Catalogue
    public static int tinhSoLuongTrang(int idCatalogue)
    {
        int soTrang ;
        int soLuongSanPhamTrenTrang = ThamSoDAO.laySoLuongSanPhamTrenTrang(1);
        int soLuong = CatalogueDAO.laySoLuongSanPham(idCatalogue);
        soTrang = soLuong / soLuongSanPhamTrenTrang;
        if(soLuong % soLuongSanPhamTrenTrang != 0)
        {
            soTrang++; 
        }
        return soTrang;
    }
    
    //Tinh so luong trang cua san pham cung loai
    public static int tinhSoLuongTrangSanPhamCungLoai(int idCatalogue)
    {
        int soTrang ;
        int soLuongSanPhamTrenTrang = ThamSoDAO.laySoLuongSanPhamTrenTrang(2);
        int soLuong = CatalogueDAO.laySoLuongSanPham(idCatalogue);
        soTrang = soLuong / soLuongSanPhamTrenTrang;
        if(soLuong % soLuongSanPhamTrenTrang != 0)
        {
            soTrang++; 
        }
        return soTrang;
    }
}
